package com.jyb.qiyefuwu.view.adpater;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.jyb.qiyefuwu.R;
import com.jyb.qiyefuwu.databinding.ItemFwFollowServiceBinding;
import com.jyb.qiyefuwu.entity.FollowServiceEntity;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.recyclerview.entity.BaseAreaEntity;
import com.swgk.core.util.DateUtils;
import com.swgk.core.util.PointUtils;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

public class FollowServiceAdpater extends BaseRecycleAdapter<FollowServiceEntity> {
    public FollowServiceAdpater(Context context) {
        super(context, R.layout.item_fw_follow_service, new ArrayList<>());
    }

    @Override
    public void convert(ItemViewHolder holder, FollowServiceEntity followServiceEntity) {
        if(holder.getmBinding() instanceof ItemFwFollowServiceBinding){
            setData((ItemFwFollowServiceBinding) holder.getmBinding(),followServiceEntity);
        }
    
    }

 
    private void setData(ItemFwFollowServiceBinding binding,FollowServiceEntity epQuoteEntity){

     
        binding.tvTime.setText(DateUtils.timestampTo13Date3(epQuoteEntity.getUpdateTime()));
         String subStr = "/";
        if (epQuoteEntity.getFirstCategoryName() != null && !epQuoteEntity.getFirstCategoryName().equals("")) {
            subStr = epQuoteEntity.getFirstCategoryName();
            if (epQuoteEntity.getSecondCategoryName() != null && !epQuoteEntity.getSecondCategoryName().equals("")) {
                String two = epQuoteEntity.getSecondCategoryName();
                subStr = subStr + "-" + two;
            }
            if (epQuoteEntity.getItemName() != null && !epQuoteEntity.getItemName().equals("")) {
                String three = epQuoteEntity.getItemName();
                subStr = subStr + "-" + three;
            }
        }
        binding.titleQuote.setText(TextUtils.isEmpty(epQuoteEntity.getServiceName())?"/":epQuoteEntity.getServiceName());
        binding.tvType1.setVisibility(View.VISIBLE);
        switch (epQuoteEntity.getPublishType()) {
            case 0:
                binding.tvType1.setText("个人");
                binding.tvType1.setBackgroundResource(R.drawable.shape_radius_1_solid_bule);
                break;
            case 1:
                binding.tvType1.setText("企业");
                binding.tvType1.setBackgroundResource(R.drawable.shape_radius_1_solid_bule);
                break;
            case 2:
                binding.tvType1.setText("团队");
                binding.tvType1.setBackgroundResource(R.drawable.shape_tage_back_orgre);
                break;
            default:
                binding.tvType1.setVisibility(View.GONE);
                break;
        }
        binding.tvCategory.setText(subStr);

        if(!TextUtils.isEmpty(epQuoteEntity.getDomesticCompaniesPrice())&&!TextUtils.isEmpty(epQuoteEntity.getMaximumPrice())){
            if(epQuoteEntity.getDomesticCompaniesPrice().equals("-1")) {
                binding.priceQuote.setText("议价");
            }else{
                binding.priceQuote.setText("¥"+ PointUtils.price(Double.parseDouble(epQuoteEntity.getDomesticCompaniesPrice()))+"起");
            }
        }else{
            binding.priceQuote.setText(epQuoteEntity.getPrice()!=-1?"¥"+PointUtils.price(epQuoteEntity.getPrice()):"议价");
        }
        String cities = epQuoteEntity.getServiceCitys();
        if (TextUtils.isEmpty(cities)) {
            binding.tvServiceCities.setText("/");
        } else {
            ArrayList<BaseAreaEntity> citysList = jsonToArrayList(cities, BaseAreaEntity.class);
            StringBuilder stringBuilder = new StringBuilder();
            for (BaseAreaEntity entity : citysList) {
                stringBuilder.append(entity.getName()).append("/");
            }
            if (stringBuilder.toString().length() > 0) {
                String cityShow = stringBuilder.toString().substring(0, stringBuilder.toString().length() - 1);
                binding.tvServiceCities.setText(cityShow);
            }
        }
        binding.imageFollow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(onItemClick!=null){
                    onItemClick.onItemClick(epQuoteEntity,-1);
                }
            }
        });
    }
    public static <T> ArrayList<T> jsonToArrayList(String json, Class<T> clazz) {
        Type type = new TypeToken<ArrayList<JsonObject>>() {
        }.getType();
        ArrayList<T> arrayList = new ArrayList<>();
        try {
            ArrayList<JsonObject> jsonObjects = new Gson().fromJson(json, type);

            for (JsonObject jsonObject : jsonObjects) {
                arrayList.add(new Gson().fromJson(jsonObject, clazz));
            }
        } catch (Exception e) {

        }

        return arrayList;
    }
}
